package ffhs.lernkartei;

import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import ffhs.lernkartei.persistence.Stack;
import ffhs.lernkartei.persistence.StacksDataSource;

public class AllStacksActivity extends ListActivity {

	// the datasource
	private StacksDataSource datasource;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all_stacks);

		datasource = new StacksDataSource(this);
		datasource.open();

		List<Stack> values = datasource.getAllStacks();

		// Use the SimpleCursorAdapter to show the
		// elements in a ListView
		ArrayAdapter<Stack> adapter = new ArrayAdapter<Stack>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);

		// Register Context Menu
		ListView list = (ListView) findViewById(android.R.id.list);
		registerForContextMenu(list);
	}

	/**
	 * Forwards the user to the AddStack Activity. 
	 * 
	 * Will be called via the onClick attribute of the buttons in all_stacks.xml
	 * 
	 * @param view
	 */
	public void onClick(View view) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<Stack> adapter = (ArrayAdapter<Stack>) getListAdapter();
		Stack stack = null;
		switch (view.getId()) {
		case R.id.AllStacks_button_add:
			Intent i = new Intent(AllStacksActivity.this,
					AddStackActivity.class);
			startActivity(i);

			// String[] comments = new String[] { "Cool", "Very nice", "Hate it"
			// };
			// int nextInt = new Random().nextInt(3);
			// // Save the new comment to the database
			// stack = datasource.createStack(comments[nextInt]);
			// adapter.add(stack);
			// break;

			// case R.id.delete:
			// if (getListAdapter().getCount() > 0) {
			// comment = (Stack) getListAdapter().getItem(0);
			// datasource.deleteStack(comment);
			// adapter.remove(comment);
			// }
			// break;
		}
		adapter.notifyDataSetChanged();
	}

	/*
	 * Create Context Menu Will be called via long click on a list item
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == android.R.id.list) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			// menu.setHeaderTitle(Countries[info.position]);
			String[] menuItems = getResources().getStringArray(
					R.array.all_stacks_context_menu);
			for (int i = 0; i < menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}

	/*
	 * Implement Context Menu
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int menuItemIndex = item.getItemId();

		ArrayAdapter<Stack> adapter = (ArrayAdapter<Stack>) getListAdapter();

		// Ask the user if they want to delete
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.AllStacks_delete_confirm_title)
				.setMessage(R.string.AllStacks_delete_confirm_really)
				.setPositiveButton(R.string.AllStacks_delete_confirm_yes,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								// Stop the activity
								AllStacksActivity.this.finish();
							}

						});

		if (getListAdapter().getCount() > 0) {
			Stack stack = (Stack) getListAdapter().getItem((int) info.id);
			datasource.deleteStack(stack);
			adapter.remove(stack);
		}

		// String[] menuItems = getResources().getStringArray(R.array.menu);
		// String menuItemName = menuItems[menuItemIndex];
		// String listItemName = Countries[info.position];
		//
		// TextView text = (TextView)findViewById(R.id.footer);
		// text.setText(String.format("Selected %s for item %s", menuItemName,
		// listItemName));
		return true;
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

}